package ext.tianma.masschange.log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import wt.org.WTUser;
import wt.session.SessionHelper;
import wt.util.WTContext;
import wt.util.WTException;
import wt.util.WTProperties;

public class SubstituteMassChangeLog {
	public static String logs_dir="";
	private static FileOutputStream fileoutputstream = null;
	private static RandomAccessFile rf=null;
	private static String CLASSNAME="";
	private static boolean VERBOSE = false;
	private static WTProperties wtProperties;
	private static Locale locale = null;

	static {

		try {
			CLASSNAME= SubstituteMassChangeLog.class.getName();
			wtProperties = WTProperties.getLocalProperties();
			locale = SessionHelper.manager.getLocale();
			VERBOSE = wtProperties.getProperty("ext.tianma.masschange.log.verbose", true);
			logs_dir = wtProperties.getProperty("wt.logs.dir", "/ptc/Windchill/logs");
			String filepath = logs_dir + File.separator + "masschange" + File.separator + "substitute";
			File path = new File(filepath);
			if(!path.exists()){
				path.mkdirs();
			}
			String cur_date =getCurDate();		
			String log_fileName =filepath + File.separator + "SubstituteMassChange_"+cur_date+".log";
			File file = new File(log_fileName);
			//如果文件不存在，则创建一个新的文件
			if(!file.exists())
			{
				//create and write to the txt file
				FileWriter writer=null;
				PrintWriter pw=null;
				//create txt file
				FileOutputStream audit_log = new FileOutputStream(log_fileName);
				String title="开始输出Substitute MassChange信息...";
				audit_log.write(title.toString().getBytes());
		        audit_log.flush();
		        audit_log.close();
			} 
			
			rf=new RandomAccessFile(log_fileName,"rw");//定义一个类RandomAccessFile的对象，并实例化
			rf.seek(rf.length());//将指针移动到文件末尾			
			rf.writeBytes("\n");
			//获得文件指针
			fileoutputstream = new FileOutputStream(rf.getFD());
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}catch (Throwable throwable) {
			throw new ExceptionInInitializerError(throwable);
		}
	}
	
	protected void finalize() throws java.lang.Throwable { 
		try {
			super.finalize();
			fileoutputstream.close();
			rf.close();	
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 根据动作和对象输出日志
	 */
	public static void writeLog(String action, String obj) throws WTException {
		if(!VERBOSE)
			return;
			
		StringBuffer stringbuffer = new StringBuffer();
		
		stringbuffer.append("INFO : ");	
		//获得当前时间
		String curTime=getCurTime();		
		stringbuffer.append(curTime);
		
		stringbuffer.append(" - ");
		//获得当前用户			
		WTUser cur_user = (WTUser)SessionHelper.manager.getPrincipal();			
		
		stringbuffer.append(cur_user.getFullName());
		
		stringbuffer.append(" | ");
		
		//输出动作					
		stringbuffer.append(action);
		
		stringbuffer.append(" - ");
		//输出对象
		stringbuffer.append(obj);
		
		stringbuffer.append("\n");
		
		try {
			fileoutputstream.write(stringbuffer.toString().getBytes());
			fileoutputstream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}								
	}
	
	/**
	 * 根据动作和对象输出日志
	 */
	public static void writeLog(String obj) throws WTException {
		if(!VERBOSE)
			return;
		
		StringBuffer stringbuffer = new StringBuffer();
		
		stringbuffer.append("INFO : ");
		
		//获得当前时间
		String curTime=getCurTime();		
		stringbuffer.append(curTime);
		
		stringbuffer.append(" - ");
		//获得当前用户			
		WTUser cur_user = (WTUser)SessionHelper.manager.getPrincipal();	
		System.out.println(cur_user.getName());
		
		stringbuffer.append(cur_user.getFullName());
		
		stringbuffer.append(" | ");
		
		//输出对象
		stringbuffer.append(obj);
		
		stringbuffer.append("\n");
		System.out.println(stringbuffer.toString());
		
		try {
			fileoutputstream.write(stringbuffer.toString().getBytes());
			fileoutputstream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}								
	}
	
	/**
	 * 根据动作和对象输出日志
	 */
	public static void writeWarnLog(String obj) throws WTException {
		if(!VERBOSE)
			return;
		
		StringBuffer stringbuffer = new StringBuffer();
		
		stringbuffer.append("WARN : ");
		
		//获得当前时间
		String curTime=getCurTime();		
		stringbuffer.append(curTime);
		
		stringbuffer.append(" - ");
		//获得当前用户			
		WTUser cur_user = (WTUser)SessionHelper.manager.getPrincipal();	
		System.out.println(cur_user.getName());
		
		stringbuffer.append(cur_user.getFullName());
		
		stringbuffer.append(" | ");
		
		//输出对象
		stringbuffer.append(obj);
		
		stringbuffer.append("\n");
		System.out.println(stringbuffer.toString());
		
		try {
			fileoutputstream.write(stringbuffer.toString().getBytes());
			fileoutputstream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}								
	}
	
	
	/**
	 * 获取当前日期，格式为yyyyMMdd
	 */
	public static String getCurDate() { 
		TimeZone tz = WTContext.getContext().getTimeZone(); 
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); 
		formatter.setTimeZone(tz); 
		Date d1=new Date(); 
		long longdate = d1.getTime(); 
		Date now = new Date(longdate); 
		String dateString = formatter.format(now); 
		return dateString; 
	}
	
	/**
	 * 获取当前时间，格式为yyyy-MM-dd HH:mm:ss
	 */
	public static String getCurTime() 
	{ 
		TimeZone tz = WTContext.getContext().getTimeZone(); 
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		formatter.setTimeZone(tz); 
		Date d1=new Date(); 
		long longdate = d1.getTime(); 
		Date now = new Date(longdate); 
		String dateString = formatter.format(now); 
		return dateString; 
	}

}
